package com.atguigu.gmall.publisher.controller;



import com.atguigu.gmall.publisher.bean.TestQuestionBean;
import com.atguigu.gmall.publisher.service.impl.TestQuestionServiceImpl;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController()
public class TestQuestionController {
    @Autowired
    private TestQuestionServiceImpl testQuestionService;
    @RequestMapping("edu/realtime/question")
    public String getTestQuestionStats(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if (date==1){
            date= DateUtil.now();
        }
        List<TestQuestionBean> testQuestionBeanList = testQuestionService.getTestQuestionBean(date);
        if (testQuestionBeanList == null) {
            return "";
        }
        ArrayList<String> questionIds = new ArrayList<>();
        ArrayList<Long> answerRightTimeCts = new ArrayList<>();
        ArrayList<Long> answerTimeCts = new ArrayList<>();
        ArrayList<Long> answerRightUvCts = new ArrayList<>();
        ArrayList<Long> answerUvCts = new ArrayList<>();
        ArrayList<Double> answerRightRates = new ArrayList<>();
        ArrayList<Double> answerRightUvRates = new ArrayList<>();

        for (int i = 0; i < testQuestionBeanList.size(); i++) {
            TestQuestionBean testQuestionBean = testQuestionBeanList.get(i);
            String questionId = testQuestionBean.getQuestionId();
            Long answerRightTimeCt = testQuestionBean.getAnswerRightTimeCount()==null?0L:testQuestionBean.getAnswerRightTimeCount();
            Long answerTimeCt = testQuestionBean.getAnswerRightTimeCount()==null?0L:testQuestionBean.getAnswerRightTimeCount();
            Long answerRightUvCt = testQuestionBean.getAnswerRightUvCount()==null?0L:testQuestionBean.getAnswerRightUvCount();
            Long answerUvCt = testQuestionBean.getAnswerRightUvCount()==null?0L:testQuestionBean.getAnswerRightUvCount();
            Double answerRightRate = testQuestionBean.getAnswerRightRate()==null?0.0:testQuestionBean.getAnswerRightRate();
            Double answerRightUvRate = testQuestionBean.getAnswerRightUvRate()==null?0.0:testQuestionBean.getAnswerRightUvRate();


            questionIds.add(questionId);
            answerRightTimeCts.add(answerRightTimeCt);
            answerTimeCts.add(answerTimeCt);
            answerRightUvCts.add(answerRightUvCt);
            answerUvCts.add(answerUvCt);
            answerRightRates.add(answerRightRate);
            answerRightUvRates.add(answerRightUvRate);

        }
        StringBuilder questionIdRows = new StringBuilder();
        StringBuilder answerRightTimeCtRows = new StringBuilder();
        StringBuilder answerTimeCtRows = new StringBuilder();
        StringBuilder answerRightUvCtRows = new StringBuilder();
        StringBuilder answerUvCtRows = new StringBuilder();
        StringBuilder answerRightRateRows = new StringBuilder();
        StringBuilder answerRightUvRateRows = new StringBuilder();
        questionIdRows.append("\"")
                .append(StringUtils.join(questionIds,"\",\""))
                .append("\"");
        answerRightTimeCtRows.append("\"")
                .append(StringUtils.join(answerRightTimeCts,"\",\""))
                .append("\"");
        answerTimeCtRows.append("\"")
                .append(StringUtils.join(answerTimeCts,"\",\""))
                .append("\"");
        answerRightUvCtRows.append("\"")
                .append(StringUtils.join(answerRightUvCts,"\",\""))
                .append("\"");
        answerUvCtRows.append("\"")
                .append(StringUtils.join(answerUvCts,"\",\""))
                .append("\"");
        answerRightRateRows.append("\"")
                .append(StringUtils.join(answerRightRates,"\",\""))
                .append("\"");
        answerRightUvRateRows.append("\"")
                .append(StringUtils.join(answerRightUvRates,"\",\""))
                .append("\"");



        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [" +
                questionIdRows +
                "    ],\n" +
                "    \"series\": [" +
                "       {\n" +
                "        \"name\": \"正确答题次数\"," +
                "        \"data\": ["+
                answerRightTimeCtRows +
                "        ]\n" +
                "      }," +
                "       {\n" +
                "        \"name\": \"答题次数\",\n" +
                "        \"data\": ["+
                answerTimeCtRows +
                "       ]\n" +
                "      },\n" +
                "       {\n" +
                "        \"name\": \"正确答题独立用户数\",\n" +
                "        \"data\": ["+
                answerTimeCtRows +
                "       ]\n" +
                "      },\n" +
                "       {\n" +
                "        \"name\": \"答题独立用户数\",\n" +
                "        \"data\": ["+
                answerUvCtRows +
                "       ]\n" +
                "      },\n" +
                "       {\n" +
                "        \"name\": \"正确率\",\n" +
                "        \"data\": ["+
                answerRightRateRows +
                "       ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"独立用户正确率\",\n" +
                "        \"data\": ["+
                answerRightUvRateRows +
                "           ]\n" +
                "      }"+
                "    ]\n" +
                "  }\n" +
                "}";
    }

}
